(function($){
	if (console === undefined)
		window.console={log:function(){}}; 
	if (console.log === undefined)
		console.log=function(){};
	
	$.Questionnaire={};
	
	$.Questions={};
	
	$.Questionnaire.previousQuestionData=[];
	
	$.Questionnaire.currentQuestionData={};
	
	$.Questionnaire.dataSendingUrl='about:blank';
	
	$.Questionnaire.sendDataTo=function(url){
			$.Questionnaire.dataSendingUrl=url;
			console.log('Data will be sent to: '+$.Questionnaire.dataSendingUrl);
		};
	
	$.Questionnaire.containerSelectors=['#container1','#container2'];
	
	$.Questionnaire.currentContainerIndex=0;
	
	$.Questionnaire.currentContainerSelector=function(){
			return $.Questionnaire.containerSelectors[$.Questionnaire.currentContainerIndex];
		};
	
	$.Questionnaire.currentContainer=function(){
			return $($.Questionnaire.currentContainerSelector());
		};
	
	$.Questionnaire.nextContainer=function(incrementIndex){
			var nextIndex=($.Questionnaire.currentContainerIndex+1)%$.Questionnaire.containerSelectors.length;
			if (incrementIndex==true)
				$.Questionnaire.currentContainerIndex=nextIndex;
			return $($.Questionnaire.containerSelectors[nextIndex]);
		};
	
	$.Questionnaire.save=function(obj){
			var newObj={};
			for (var n in obj)
				newObj[(''+n).toLowerCase().replace(/[^a-z0-9_]/g,'-')]=obj[n];
			$.extend($.Questionnaire.currentQuestionData,newObj);
			console.log('This data is being saved: '+JSON.stringify(newObj));
			console.log('Total data at this point: '+JSON.stringify($.Questionnaire.currentQuestionData));
		};
	
	$.Questionnaire.end=function(){
			$.Questionnaire.previousQuestionData.push($.Questionnaire.currentQuestionData);
			$.Questionnaire.currentQuestionData={};
			(function(){
					if ($.Questionnaire.previousQuestionData.length===0)
					{
						$.Questionnaire.goToFirst();
						return;
					}
					var argCallee=arguments.callee;
					var questionData=$.Questionnaire.previousQuestionData.shift();
					console.log(JSON.stringify(questionData));
					if ($.Questionnaire.dataSendingUrl!=='about:blank')
						$.ajax({
								url: $.Questionnaire.dataSendingUrl,
								context: document.body,
								cache: false,
								data: questionData,
								type: 'POST',
								dataType: 'json',
								async: true,
								success: function(responseData){
										console.log('Data sent successfully.');
										console.log(responseData);
										argCallee();
									},
								error: function(){
										console.log('Aborted, data has not been sent successfully.');
										$.Questionnaire.previousQuestionData.push(questionData);
									}
							});
				})();
		};
	
	$.Questionnaire.next=function(questionName){
			if (!(questionName in $.Questions))
				throw new Error('Invalid question name passed into $.Questionnaire.next: '+questionName);
			var question=$.Questions[questionName];
			if (question.question_template_selector!=='' && question.question_template_selector!==undefined)
			{
				var nextContainer=$.Questionnaire.nextContainer(true);
				nextContainer.html('');
				if (!$(question.question_template_selector).length)
					throw new Error('No elements found to match question_template_selector defined for question "'+questionName+'": '+question.question_template_selector);
				var tmpl=$.tmpl('<span>'+$(question.question_template_selector).html()+'</span>',question);
				if (!tmpl.find(question.show_answers_selector).length)
					throw new Error('No elements found to match show_answers_selector defined for question "'+questionName+'": '+question.show_answers_selector);
				tmpl.find(question.show_answers_selector).attr('onclick','\
						$(this).unbind("click");\
						$.Questionnaire.showAnswer("'+questionName+'");\
					');
				nextContainer.append(tmpl.html());
				nextContainer.trigger('create');
				$.mobile.changePage($.Questionnaire.currentContainerSelector());
			}
			else
			{
				$.Questionnaire.showAnswer(questionName);
			}
		};
	
	$.Questionnaire.showAnswer=function(questionName){
			var question=$.Questions[questionName];
			var nextContainer=$.Questionnaire.nextContainer(true);
			nextContainer.html('');
			if (!$(question.answer_template_selector).length)
				throw new Error('No elements found to match answer_template_selector defined for question "'+questionName+'": '+question.answer_template_selector);
			var tmpl=$.tmpl('<span>'+$(question.answer_template_selector).html()+'</span>',question);
			if (!tmpl.find(question.next_question_selector).length)
				throw new Error('No elements found to match next_question_selector defined for answer template to "'+questionName+'": '+question.next_question_selector);
			tmpl.find(question.next_question_selector).attr('onclick','\
					$(this).unbind("click");\
					$.Questions["'+questionName+'"].process_values($("'+$.Questionnaire.currentContainerSelector()+'"),$(this));\
				');
			nextContainer.append(tmpl.html());
			nextContainer.trigger('create');
			$.mobile.changePage($.Questionnaire.currentContainerSelector());
		};
		
	$.Questionnaire.goToFirst=function(){};
	
	$.Questionnaire.first=function(questionName, showAnswer){
			if (!showAnswer)
				$.Questionnaire.goToFirst=function(){
						$.Questionnaire.next(questionName);
					}
			else
				$.Questionnaire.goToFirst=function(){
						$.Questionnaire.showAnswer(questionName);
					}
			$($.Questionnaire.goToFirst);
		};
	
})(jQuery);